%macro prnt (program=, disname=, idisname=,ds_list=);

Data adae;
   set adam.adae;
   where saffl='Y' and TRTEMFL='Y' and AESER='Y';
   keep usubjid saffl trtemfl trta aebodsys aebodsys aedecod;
   run;
***Read in ADSL Data***;
data adsl;
   set adam.adsl(where=(saffl='Y'));
   part=scan(actarmcd, 1, '/');
   trt1=substr(part, 1, 1);
   trt2=substr(part, 2, 1);
   trt3=substr(part, 3, 1);
   if trt1='A' or TRT2='A' or TRT3='A'  then trta='A'; 
output;
  if trt1='B' or TRT2='B' or TRT3='B' then trta='B'; 
output;  
if trt1='C' or TRT2='C' or TRT3='C'  then trta='C'; 
output;
run;

data adsl;
   set adsl;
   where trta ne '';run;
/*proc sort data=adsl nodupkey dupout=dum1;*/
/*   by usubjid trta;run; */
proc sql;
   create table pct as 
   select count(distinct usubjid) as bign, trta
   from adsl
   group by trta;
quit;
data pct;
   set pct;
   if trta='A' then ntrt=1;
   if trta='B' then ntrt=2;
   if trta='C' then ntrt=3;run;
data _null_ ;
set pct;
call symput("trt"||strip(put(ntrt, best.)), strip(put(bign, 8.)));
run;
%put &trt1 &trt2 &trt3;
proc sql;
   create table row1 as 
   select trta, aebodsys, aedecod, count(distinct usubjid) as count, "ANY SUPAE" as label, 1 as ord
   from adae
   group by trta
   outer union corr

    select trta,aebodsys, aedecod, count(distinct usubjid) as count, "  "||aebodsys as label,2 as ord 
   from adae
   group by trta, aebodsys
   outer union corr

   select trta,aebodsys, aedecod, count(distinct usubjid) as count, "    "||aedecod as label, 3 as ord 
   from adae
   group by trta, aebodsys, aedecod;
   quit;
***Nuber of subjects by trt***;
proc sort data=adae out=tot nodupkey;
   by trta usubjid;
run;
proc freq data=tot ;
tables trta/out=freq1;run;
proc sort data=adae out=soc nodupkey;
   by trta usubjid aebodsys;
run;
proc freq data=soc ;
tables trta*aebodsys/out=freq2;run;
proc sort data=adae out=pt nodupkey;
   by trta usubjid aebodsys aedecod;
run;
proc freq data=pt ;
tables trta*aebodsys*aedecod/out=freq3;run;
data total;
   set freq1 freq2 freq3;run;
proc sort data=total;
   by trta;
run;

proc sort data=pct;
   by trta;
run;
***Nuber of events by trt***;
proc sort data=adae out=eve ;
   by trta usubjid;
run;
proc freq data=eve ;
tables trta/out=cnt1;run;
proc sort data=adae out=soc_eve;
   by trta usubjid aebodsys;
run;
proc freq data=soc_eve ;
tables trta*aebodsys/out=cnt2;run;
proc sort data=adae out=pt_eve ;
   by trta usubjid aebodsys aedecod;
run;
proc freq data=pt_eve ;
tables trta*aebodsys*aedecod/out=cnt3;run;
data total_eve(keep=trta count);
   set cnt1 cnt2 cnt3;run;
proc sort data=total_eve(rename=(count=cnt));
   by trta;
run;
data table;
   merge total total_eve pct;
   by trta;
   if count not in(. 0) then stats='   '||put(count, 4.)||' ('||put((count/bign*100), 5.1)||')' ||'  '||put(count, 4.);
run;

proc sort data=table nodupkey dupout=dum;
   by trta aebodsys aedecod;

run;
proc sort data=table ;
   by aebodsys aedecod;

run;
proc transpose data=table out=table1;
   by aebodsys aedecod;
   var stats;
   id trta;
run;

data final;
   length col1 $200;
   set table1;
/*   by aebodsys;*/
   if aedecod ^='' then col1="  "||strip(aedecod);
   if col1='' and aebodsys ne '' then col1=strip(aebodsys);
   if col1='' then delete;
   page=ceil(_n_/20);
run;

***Printing using proc report;
******************************************************************************************************;
title3 "Table 14.3.1.3.1";

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T14.3.1.3.1.AES.rtf";


%open_rtf;
%empty_ds_fix(final);
%put nobs=&nb_obs;
title4 "Summary of Serious Study Product Use-Emergent Adverse Events by System Organ Class and MedDRA Preferred Term - Part A";
title5 "(Safety Population)";

%footer1(foot1=1, ds_list=&ds_list., Program=&program.);
footnote3 j=l "Note(s): For the subject counts, each Study Product Use-Emergent adverse event is counted only once for each subject within each System Organ Class and";
footnote4 j=l "MedDRA Preferred Term.";
proc report data=final nowd split='~' missing;
 
column page aebodsys ("\line System Organ Class~    MedDRA Preferred Term"  col1)
       ("\line Study Product A~(N=&trt1)" A) ("\line Study Product B~(N=&trt2)" B) (" \line Study Product C~(N=&trt3)" C);
  
   



 define page         / " " order order=internal noprint;
 define aebodsys      / " " order order=internal noprint;
/* define sort1        / " " order descending order=internal noprint;*/
/* define level1_label / " " order order=internal noprint;*/
/* define level2       / " " order order=internal noprint;*/
/* define sort2        / " " order descending order=internal noprint;*/
 define col1 /display "~ ~ " order=internal style(column)=[cellwidth=3.5in just=left] style(header)=[just=left];
define A /display "~Subjects   Events~[n(%)]  [n]" order=internal style(column)=[cellwidth=2.0in just=left] style(header)=[just=center];
define B /display "~Subjects   Events~[n(%)]  [n]" order=internal style(column)=[cellwidth=2.0in just=left] style(header)=[just=center];
define C /display "~Subjects   Events~[n(%)]  [n]" order=internal style(column)=[cellwidth=2.0in just=left] style(header)=[just=center];


 break after page  / page;

 
 compute before aebodsys;
   line '';
   line '';
   line "No serious adverse events were reported.";
 endcomp;
 
run;

%close_rtf;
%mend prnt;
%prnt(program=T14_03_01_03_01_AES, disname=14.3.1.3.1, idisname=14.3.1.3.1, ds_list=ADAE );


%clrw;


